import Vue from 'vue'
import Vuex from 'vuex'
import {
	insertMsg,
	insertManyMsg
} from '../mySqlite.js'
Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		isLogin: uni.getStorageSync("loginUserInfo") ? true : false,
		// 存放当前登录用户的用户信息
		loginUserInfo: uni.getStorageSync("loginUserInfo") || {"id":"2646883922"},

		chatList: [{
			userId: "1171702529", //用户id
			name: "老王", //用户名
			userTag: "", //名字后面的标签，标签为好友的设置
			isOnLine: true, // 是否在线
			avatar: "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1061533119,2379003141&fm=26&gp=0.jpg", //用户头像
			latelyMsg: "", //最近的一条消息
			latelyTime: "22:20", //最新消息的时间
			unreadNum: 100, //未读消息数
			isSuccess: false, //消息是否发送成功
			isDoNotDisturb: true, //是否勿扰 勿扰则未读消息个数消息显示为灰色
		}, {
			userId: "1171702529",
			name: "凯尔", //名字
			userTag: "", //名字后面的标签
			isOnLine: true, // 是否在线
			avatar: "https://ossweb-img.qq.com/images/lol/web201310/skin/big10001.jpg", //头像
			latelyMsg: "我已天理为凭，踏入这片荒芜，不再受凡人的枷锁遏制。我已天理为凭，踏入这片荒芜，不再受凡人的枷锁遏制。", //最新消息
			latelyTime: "22:20", //最新消息的时间
			unreadNum: "5", //未读消息的个数
			isSuccess: false, //消息是否发送成功
			isDoNotDisturb: true, //是否勿扰 勿扰则未读消息个数消息显示为灰色
		}, {
			userId: "1171702529",
			name: "瓦洛兰之盾-塔里克",
			userTag: "战士",
			isOnLine: true,
			avatar: "https://ossweb-img.qq.com/images/lol/img/champion/Taric.png",
			latelyMsg: "塔里克是保护者星灵，用超乎寻常的力量守护着符文之地的生命、仁爱以及万物之美。塔里克由于渎职而被放逐，离开了祖国德玛西亚，前去攀登巨神峰寻找救赎，但他找到的却是来自星界的更高层的召唤。现在的塔里克与古代巨神族的神力相融合，以瓦洛兰之盾的身份，永不疲倦地警惕着阴险狡诈的虚空腐化之力。",
			latelyTime: "22:20",
			unreadNum: 10,
			isSuccess: true,
			isDoNotDisturb: false,
		}, {
			userId: "1171702529",
			name: "莫甘娜",
			userTag: "",
			isOnLine: true,
			avatar: "https://ossweb-img.qq.com/images/lol/img/champion/Morgana.png",
			latelyMsg: "凯尔，你被自己的光芒变的盲目！",
			latelyTime: "22:20",
			unreadNum: 10,
			isSuccess: true,
			isDoNotDisturb: false,
		}, {
			userId: "1171702529",
			name: "",
			userTag: "断开连接...",
			isOnLine: false,
			avatar: "https://ossweb-img.qq.com/images/lol/web201310/skin/big81007.jpg",
			latelyMsg: "等我回来一个打十个",
			latelyTime: "22:20",
			unreadNum: 10,
			isSuccess: true,
			isDoNotDisturb: true,
		}, {
			userId: "1171702529",
			name: "瓦罗兰大陆-睡衣守护者-新手保护营aaaaaaaaaa",
			userTag: "6人",
			isOnLine: true,
			avatar: "https://ossweb-img.qq.com/images/lol/web201310/skin/big81020.jpg",
			latelyMsg: ' 伊泽瑞尔：<text class="cuIcon-locationfill text-orange margin-right-xs"></text> 传送中...',
			latelyTime: "22:20",
			unreadNum: 10,
			isSuccess: true,
			isDoNotDisturb: true,
		}, {
			userId: "1171702529",
			name: "凯尔", //名字
			userTag: "", //名字后面的标签
			isOnLine: true, // 是否在线
			avatar: "https://ossweb-img.qq.com/images/lol/web201310/skin/big10001.jpg", //头像
			latelyMsg: "我已天理为凭，踏入这片荒芜，不再受凡人的枷锁遏制。我已天理为凭，踏入这片荒芜，不再受凡人的枷锁遏制。", //最新消息
			latelyTime: "22:20",
			unreadNum: 10,
			isSuccess: false, //消息是否发送成功
			isDoNotDisturb: true, //是否勿扰 勿扰则未读消息个数消息显示为灰色
		}, {
			userId: "1171702529",
			name: "瓦洛兰之盾-塔里克",
			userTag: "战士",
			isOnLine: true,
			avatar: "https://ossweb-img.qq.com/images/lol/img/champion/Taric.png",
			latelyMsg: "塔里克是保护者星灵，用超乎寻常的力量守护着符文之地的生命、仁爱以及万物之美。塔里克由于渎职而被放逐，离开了祖国德玛西亚，前去攀登巨神峰寻找救赎，但他找到的却是来自星界的更高层的召唤。现在的塔里克与古代巨神族的神力相融合，以瓦洛兰之盾的身份，永不疲倦地警惕着阴险狡诈的虚空腐化之力。",
			latelyTime: "22:20",
			unreadNum: 10,
			isSuccess: true,
			isDoNotDisturb: false,
		}, {
			userId: "1171702529",
			name: "莫甘娜",
			userTag: "",
			isOnLine: true,
			avatar: "https://ossweb-img.qq.com/images/lol/img/champion/Morgana.png",
			latelyMsg: "凯尔，你被自己的光芒变的盲目！",
			latelyTime: "22:20",
			unreadNum: 10,
			isSuccess: true,
			isDoNotDisturb: false,
		}, {
			userId: "1171702529",
			name: "",
			userTag: "断开连接...",
			isOnLine: false,
			avatar: "https://ossweb-img.qq.com/images/lol/web201310/skin/big81007.jpg",
			latelyMsg: "等我回来一个打十个",
			latelyTime: "22:20",
			unreadNum: 10,
			isSuccess: true,
			isDoNotDisturb: true,
		}, {
			userId: "1171702529",
			name: "莫甘娜",
			userTag: "",
			isOnLine: true,
			avatar: "https://ossweb-img.qq.com/images/lol/img/champion/Morgana.png",
			latelyMsg: "凯尔，你被自己的光芒变的盲目！",
			latelyTime: "22:20",
			unreadNum: 10,
			isSuccess: true,
			isDoNotDisturb: false,
		}, {
			userId: "1171702529",
			name: "",
			userTag: "断开连接...",
			isOnLine: false,
			avatar: "https://ossweb-img.qq.com/images/lol/web201310/skin/big81007.jpg",
			latelyMsg: "等我回来一个打十个",
			latelyTime: "22:20",
			unreadNum: 10,
			isSuccess: true,
			isDoNotDisturb: true,
		}],
	},
	mutations: {
		//登录
		login(state, payload) {
			if (payload) {
				state.loginUserInfo = payload
				// console.log(state.loginUserInfo)
				uni.setStorageSync("loginUserInfo", state.loginUserInfo)
			}
			state.isLogin = true;
		},
		// 注销
		logout(state) {
			// 后端保存朱晓星消息
			// uni.request({
			// 	url: this.url.redis.logout,
			// 	method: 'POST',
			// 	data: JSON.stringify(state.loginUserInfo.id),
			// 	header: {
			// 		"content-type": "application/json;charset=UTF-8"
			// 	},
			// 	success: (res) => {
			// 		console.log(res)
			// 	},
			// 	fail: (res) => {
			// 		that.showTips(3000, res.data.msg, "error")
			// 		console.log(res)
			// 	}
			// });
			state.loginUserInfo = {}
			state.sessionId = null
			uni.removeStorageSync('loginUserInfo');
			uni.removeStorageSync('sessionId');
			state.isLogin = false;
		},
		
		// TODO 这里需要修改！！！
		/**
		 * msg：{
				 content: content, //动作类型
				 type: type,//聊天消息类型
				 time: Date.parse(new Date()),//时间戳
				 isMe: true,//是否本人
			 }
		 * 
		 */
		insertUnreadMsg(state, userId, msg,isMany) {
			//msg.time为时间戳格式的时间，转化为需要的字符串格式
			console.log(msg)
			for (var i = 0; i < state.chatList.length; i++) {
				if (state.chatList[i].userId == userId) {
					if(isMany){
						insertManyMsg(tableName, msg, callback)
						state.chatList[i].unreadNum = parseInt(state.chatList[i].unreadNum) + msg.length;
						var time = new Date(msg[msg.length-1].time)
						time = time.getHours() + ":" + time.getMinutes()+":"+time.getSeconds();
						state.chatList[i].latelyMsg= msg[msg.length-1].content
						state.chatList[i].latelyTime= time
					}else{
						insertMsg(tableName, msg, callback)
						state.chatList[i].unreadNum = parseInt(state.chatList[i].unreadNum) + 1;
						var time = new Date(msg.time)
						time = time.getHours() + ":" + time.getMinutes()+":"+time.getSeconds();
						state.chatList[i].latelyMsg= msg.content
						state.chatList[i].latelyTime= time
					}
					uni.setStorageSync("chatList", state.chatList)
					return;
				}
			}
			//没找到，添加到消息列表中，一个新的对象
			//TODO 去redis上找到这个用户的头像和名字
			uni.request({
				url: that.url.tuser.getUserInfoById + "/" + userId,
				method: 'GET',
				header: {
					"content-type": "application/json;charset=UTF-8"
				},
				success: (res) => {
					console.log(res)
					if (res.data.msg == "success") {
						user = res.data.user;
						state.chatList.push({
							userId: userId,
							name: user.name,
							userTag: "",
							isOnLine: true,
							avatar: user.avatar,
							latelyMsg: msg.content,
							latelyTime: msg.time,
							unreadNum: 1,
							isSuccess: true,
							isDoNotDisturb: true,
						})

					} else {
						that.showTips(3000, res.data.msg, "error")
						console.log(res.data.msg);
						that.password = ""
					}

				},
				fail: (res) => {
					that.showTips(3000, res.data.msg, "error")
					console.log(res)
				}
			});
		},
		// TODO 这里需要修改！！！
		//删除一条消息列表
		delChatList(state, i) {
			state.chatList.remove(i);
		},
		//退出一个聊天时，删除会话中的未读消息内容
		//这个顺序与展示的数据相同
		readChatList(state, i) {
			state.chatList[i].unreadNum=0;
		},
		//TODO 重新设置最近的消息，在退出聊天的时候，或者通过websocket获取聊天记录的时候得到
		//退出聊天的时候，存入当前展示的最后一条消息。
		resetlatelyMsg(state,obj){
			var userId = obj.userId
			var msg = obj.msg
			console.log(obj)
			for (var i = 0; i < state.chatList.length; i++) {
				if (state.chatList[i].userId == userId) {
					state.chatList[i].latelyMsg=msg.content; //最近的一条消息
					state.chatList[i].latelyTime=msg.time; //最新消息的时间
					return;
				}
			}
		}
	},
	actions: {

	}
})

export default store
